﻿using System.Data;
using System.Data.OleDb;
using System.Text;
using Framework;

namespace Enterprise
{
    /// <summary>
    /// 系统设置管理
    /// </summary>
    public class SettingService
    {
        /// <summary>
        /// 系统项设置
        /// </summary>
        /// <param name="colunm"></param>
        /// <param name="value"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public int Set(string colunm, string value, string language)
        {
            var sbSql = new StringBuilder();
            sbSql.Append("select count(*) from TbSetting  ");
            sbSql.Append(" where CoLanguage=@CoLanguage;");
            OleDbParameter[] parameters =
                {
               new OleDbParameter("@CoLanguage", OleDbType.VarChar,255)
                };
            parameters[0].Value = language;
            var count = DbHelperOleDb.GetSingle(sbSql.ToString(), parameters).SafeToInt();
            sbSql.Clear();
            if (count > 0)
            {
                sbSql.Append("update TbSetting set ");
                sbSql.AppendFormat("{0}=@{0}", colunm);
                sbSql.Append(" where CoLanguage=@CoLanguage;");
            }
            else
            {
                sbSql.AppendFormat("insert into TbSetting(CoLanguage,{0})", colunm);
                sbSql.AppendFormat(" values(@CoLanguage,@{0});", colunm);
            }
            OleDbParameter[] parametersOne =
                {
                    new OleDbParameter("@"+colunm, OleDbType.LongVarWChar),
                    new OleDbParameter("@CoLanguage", OleDbType.VarChar,255)
                };
            parametersOne[0].Value = value;
            parametersOne[1].Value = language;
            var obj = DbHelperOleDb.ExecuteSql(sbSql.ToString(), parametersOne).SafeToInt();
            return obj;
        }

        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="colunm"> </param>
        /// <param name="language"> </param>
        /// <returns></returns>
        public DataRow GetByLanguage(string colunm, string language)
        {
            Check.Argument.IsSafeSql(colunm, "SQL语句");
            OleDbParameter[] parameters =
                {
                   new OleDbParameter("@CoLanguage",OleDbType.VarChar,255)
                };
            parameters[0].Value = language;
            DataRowCollection drc = DbHelperOleDb.Query("select {0},CoLanguage  from TbSetting  where CoLanguage=@CoLanguage".FormatWith(colunm), parameters).Tables["ds"].Rows;
            if (drc.Count <= 0)
            {
                var sbSql = new StringBuilder();
                sbSql.AppendFormat("insert into TbSetting(CoLanguage,{0})", colunm);
                sbSql.AppendFormat(" values(@CoLanguage,@{0});", colunm);
                OleDbParameter[] parametersOne =
                {
                    new OleDbParameter("@"+colunm, OleDbType.LongVarWChar),
                    new OleDbParameter("@CoLanguage", OleDbType.VarChar,255)
                };
                parametersOne[0].Value = "";
                parametersOne[1].Value = language;
                DbHelperOleDb.ExecuteSql(sbSql.ToString(), parametersOne).SafeToInt();
                drc = DbHelperOleDb.Query("select {0},CoLanguage  from TbSetting  where CoLanguage=@CoLanguage".FormatWith(colunm), parameters).Tables["ds"].Rows;
            }
            return drc[0];
        }
        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="language"> </param>
        /// <returns></returns>
        public DataRow GetByLanguage(string language)
        {
            OleDbParameter[] parameters =
                {
                    new OleDbParameter("@CoLanguage", OleDbType.VarChar, 255)
                };
            parameters[0].Value = language;

            var sbSql = new StringBuilder();
            var rows = DbHelperOleDb.Query("select * from TbSetting where CoLanguage=@CoLanguage", parameters).Tables["ds"].Rows;
            if (rows.Count < 1)
            {
                sbSql.AppendFormat("insert into TbSetting(CoLanguage)");
                sbSql.AppendFormat(" values(@CoLanguage);");
                DbHelperOleDb.ExecuteSql(sbSql.ToString(), parameters);
                rows = DbHelperOleDb.Query("select * from TbSetting where CoLanguage=@CoLanguage", parameters).Tables["ds"].Rows;
            }
            return rows[0];
        }
    }
}
